﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Client.Messages;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using System.Runtime.Serialization;
using System.Threading;
using System.IO;

namespace CRM_Deletion_Utility
{
    class Program
    {
        static void Main(string[] args)
        {
            //load query
            string xmlFetchQuery = File.ReadAllText(@".\FetchXML.xml");
            string xmlFetchCountQuery = xmlFetchQuery.Replace("<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>", "<fetch distinct='false' mapping='logical' aggregate='true'>");
            //pull count from CRM
            args[0] = args[0].Replace("/ConnectionString:", "");

            //you can just add your connection string in here, or pass it in via string args into the console app

            //Microsoft.Xrm.Client.CrmConnection connection = CrmConnection.Parse(args[0]);
            Console.WriteLine("Connecting...");
            Microsoft.Xrm.Client.CrmConnection connection = CrmConnection.Parse(@"Url=https://myorgnamehere.crm.dynamics.com; Username=jmiley@techairgroup.com; Password=MYPASSWORDHERE;");

            
            Console.WriteLine("Connecting...");

            
            
            // Obtain an organization service proxy.
            // The using statement assures that the service proxy will be properly disposed.
            using (var _orgService = new OrganizationService(connection))
            {
               WhoAmIResponse resp = (WhoAmIResponse)_orgService.Execute(new WhoAmIRequest());
                Console.WriteLine("My UserID is: " + resp.UserId.ToString());
                Console.Write("DONE!!");
                Console.ReadKey();

            }   
            
        }
    }
}
